package com.store.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.HPTOpenBean;
import com.store.app.utils.IDUtil;

import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * author lushan
 * on 2017/3/23 0023 16:16
 * 火拼团开团适配器
 */

public class TuanCreatAdapter extends BaseAdapter{

    private Context mContext;
    private List<HPTOpenBean.ListBean> mList;
    private Map<String, String> urlMap;
    public TuanCreatAdapter(Context context, List<HPTOpenBean.ListBean> list,Map<String, String> urlMap) {
        this.mContext = context;
        this.mList = list;
        this.urlMap = urlMap;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        Holder holder = null;
        if (view == null) {
            holder = new Holder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_tuan_creat, null);
            holder.txt_order_number = (TextView) view.findViewById(R.id.txt_order_number);
            holder.txt_statue = (TextView) view.findViewById(R.id.txt_statue);
            holder.t_good_name = (TextView) view.findViewById(R.id.t_good_name);
            holder.tv_dbrc = (TextView) view.findViewById(R.id.tv_dbrc);
            holder.img_good = (ImageView) view.findViewById(R.id.img_good);
            holder.t_chengtuan_price = (TextView) view.findViewById(R.id.t_chengtuan_price);
            holder.btn_ckdd_order= (Button) view.findViewById(R.id.btn_look_order);//查看订单
            holder.btn_cancle_order= (Button) view.findViewById(R.id.btn_cancle_order);
            holder.btn_confirm_order= (Button) view.findViewById(R.id.btn_confirm_order);//确认收货隐藏
            holder.tv_jzsj= (TextView) view.findViewById(R.id.tv_jzsj);//截止时间
            holder.tv_shfs= (TextView) view.findViewById(R.id.tv_shfs);//收货方式
            holder.tv_shdz= (TextView) view.findViewById(R.id.tv_shdz);//收货地址
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        HPTOpenBean.ListBean bean = (HPTOpenBean.ListBean) getItem(position);
        holder.txt_order_number.setText( bean.getActivity_no());
        holder.t_good_name.setText(bean.getTitle());
        holder.txt_statue.setText(IDUtil.hptActivityStatus(bean.getStatus()));
        holder.tv_jzsj.setText(bean.getExpiried_date());//截止时间
        holder.tv_dbrc.setText(bean.getMin_num());//达标人次
        //holder.t_chengtuan_price.setText("¥" +bean.getSale_price());//成团价格
        holder.tv_shdz.setText(bean.getAddress());//收货地址
        String s_shfs="";
        if("pick_up".equals(bean.getReceived_mode())){
            s_shfs="门店自提";
        }else {
            s_shfs="送货上门";
        }
        holder.tv_shfs.setText(s_shfs);//收货方式
        if("delivered".equals(bean.getStatus())) {
            holder.btn_confirm_order.setVisibility(View.VISIBLE);
        }else {
            holder.btn_confirm_order.setVisibility(View.GONE);
        }
        holder.btn_cancle_order.setVisibility(View.GONE);
        String picinfo = bean.getGoods_json_data();
        if (!TextUtils.isEmpty(picinfo)) {
            try {
                JSONObject jsonObject = new JSONObject(picinfo);
                String img = jsonObject.getString("path_id");
                img = urlMap.get(img);
                ImageLoader.getInstance().displayImage(img, holder.img_good, options);
            } catch (Exception e) {

            }
        }

        holder.btn_ckdd_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickOrde != null) {
                    onClickOrde.lockorderClick(mList.get(position));
                }
            }
        });
        holder.btn_confirm_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickOrde != null) {
                    onClickOrde.confirmClick(mList.get(position));
                }
            }
        });

        return view;
    }

    class Holder {
        TextView txt_order_number;//订单编号
        TextView txt_statue;//发货方式
        ImageView img_good;//商品图片
        TextView t_good_name;//商品名称
        TextView tv_dbrc;//达标人次
        TextView tv_jzsj;//截止时间
        TextView t_chengtuan_price;//成团金额
        TextView tv_shfs;//收货方式
        TextView tv_shdz;//收货地址
        Button btn_ckdd_order,btn_cancle_order,btn_confirm_order;
    }

    //伙拼团监听
    public interface OnClickOrder{
        void confirmClick(HPTOpenBean.ListBean bean);
        void lockorderClick(HPTOpenBean.ListBean bean);
    }
    private OnClickOrder onClickOrde;
    public void setOnClickOrder(OnClickOrder onClickOrder_type2){
        this.onClickOrde=onClickOrder_type2;
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.default_picture_s)
            .showImageOnFail(R.drawable.default_picture_s)
            .showStubImage(R.drawable.default_picture_s)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}
